package domain.searchEngine;

import java.io.Serializable;

import presentation.UIPost;

/**
 * class SearchResult
 * holds the wanted data about a searched post. 
 * 
 * each search result is created per one specific search.
 * therefore it is used as a read-only class, 
 * and cannot be changed from the moment it is created.
 */
public class SearchResult implements Serializable, Comparable<Object>
{
	
	private static final long serialVersionUID = -7375752868046064641L ;
	private UIPost post;
	private float score;
	
	/**
	 * constructor.
	 * @param post - a post that matches the search
	 * @param score - the post's score in the search
	 */
	public SearchResult(UIPost post, float score)
	{
		this.post = post ;
		this.score = score ;
	}

	/**
	 * @return the post as read-only
	 */
	public UIPost getPost()
	{
		return this.post ;
	}
	
	/**
	 * @return the post's score in the current search
	 */
	public float getScore()
	{
		return this.score ;
	}


	/** 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(Object otherRes)
	{
		if(this.score > ((SearchResult)otherRes).getScore()){
			return -1; //before otherRes - when the rank is higher (=better)
		}
		else if(this.score < ((SearchResult)otherRes).getScore()){
			return 1; //after otherRes - when the rank is lower (=worse)
		}
		else return 0; //equals to otherRes
	}

	

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true ;
		if (obj == null)
			return false ;
		if (getClass() != obj.getClass())
			return false ;
		final SearchResult other = (SearchResult)obj ;
		if (this.score != other.getScore())
			return false ;
		return true ;
	}
	
	
	
}
